import Vue from 'vue'
import Router from 'vue-router'
import axios from 'axios';
Vue.use(Router)
/* Layout */
import Layout from '@/layout'
import { MessageBox } from 'element-ui';
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
 roles: ['admin','editor']    control the page roles (you can set multiple roles)
 title: 'title'               the name show in sidebar and breadcrumb (recommend set)
 icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
 breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
 activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
 }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/face',
    component: () => import('@/views/face/face'),
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: '提交材料批阅',
      component: () => import('@/views/page/superAdmin/form/review'),
      meta: {title: '提交材料批阅', icon: 'dashboard'}
    }]
  },
  // {
  //   path: '/reviewMaterials',
  //   component: Layout,
  //   redirect: '/reviewMaterials/list',
  //   name: '提交材料批阅',
  //   meta: { title: '提交材料批阅', icon: 'el-icon-s-help' },
  //   children: [
  //     {
  //       path: 'list',
  //       name: '提交材料批阅',
  //       component: () => import('@/views/page/superAdmin/form/review'),
  //       meta: { title: '提交材料批阅', icon: 'table' }
  //     },
  //   ]
  // },
  {
    path: '/adminTeacher',
    component: Layout,
    redirect: '/adminTeacher/list',
    name: '教师信息管理',
    meta: { title: '教师信息管理', icon: 'el-icon-s-help' },
    beforeEnter: async (to, from, next) => {
      try {
        const response = await axios.post(`${process.env.VUE_APP_BASE_API}/admservice/adm-superadministrator/checkIfSuperAdmin`);
        console.log(response.data)
        if (response.data) {
          // 是超级管理员，可以访问
          next();
        } else {
          // 不是超级管理员，不能访问
          MessageBox.alert('权限不足', '提示', {
            type: 'error'
          });
          next(false);
        }
      } catch (error) {
        console.error(error);
        MessageBox.alert('服务器暂时崩溃，请耐心等待或联系工作人员', '提示', {
          type: 'error'
        });
        next(false);
      }
    },
    hidden:true,
    children: [
      {
        path: 'list',
        name: '教师信息列表',
        component: () => import('@/views/page/adminTeacher/show'),
        meta: { title: '教师列表', icon: 'table' },
        hidden:true,
      },
      {
        path: 'addTeacher',
        name: '添加教师信息',
        component: () => import('@/views/page/adminTeacher/addTeacher'),
        meta: { title: '添加教师信息', icon: 'el-icon-thumb' },
        hidden:true,
      },
      {
        path: 'updateTeacher/:id',
        name: '修改教师信息',
        component: () => import('@/views/page/adminTeacher/addTeacher'),
        meta: { title: '修改教师信息', noCache:true },
        hidden:true
      }
    ]
  },
  {
    path: '/genTeacher',
    component: Layout,
    redirect: '/gen/list',
    name: '总评管理',
    meta: { title: '总评管理', icon: 'el-icon-s-help' },
    beforeEnter: async (to, from, next) => {
      try {
        const response = await axios.post(`${process.env.VUE_APP_BASE_API}/admservice/adm-superadministrator/checkIfSuperAdmin`);
        console.log(response.data)
        if (response.data) {
          // 是超级管理员，可以访问
          next();
        } else {
          // 不是超级管理员，不能访问
          MessageBox.alert('权限不足', '提示', {
            type: 'error'
          });
          next(false);
        }
      } catch (error) {
        console.error(error);
        MessageBox.alert('服务器暂时崩溃，请耐心等待或联系工作人员', '提示', {
          type: 'error'
        });
        next(false);
      }
    },
    hidden:true,
    children: [
      {
        path: 'menu4',
        component: () => import('@/views/page/superAdmin/general/madeGen'),
        name: 'Menu4',
        meta: { title: '生成总评', icon: 'el-icon-news' },
        hidden:true,
      },
      {
        path: 'menu5',
        component: () => import('@/views/page/superAdmin/general/showGen'),
        name: 'Menu5',
        meta: { title: '总评', icon: 'el-icon-trophy' },
        hidden:true,
      },
    ]
  },
  {
    path: '/adminSuper',
    component: Layout,
    redirect: '/adminSuper/list',
    name: '审核员信息管理',
    meta: { title: '审核员信息管理', icon: 'el-icon-s-help' },
    beforeEnter: async (to, from, next) => {
      try {
        const response = await axios.post(`${process.env.VUE_APP_BASE_API}/admservice/adm-superadministrator/checkIfSuperAdmin`);
        console.log(response.data)
        if (response.data) {
          // 是超级管理员，可以访问
          next();
        } else {
          // 不是超级管理员，不能访问
          MessageBox.alert('权限不足', '提示', {
            type: 'error'
          });
          next(false);
        }
      } catch (error) {
        console.error(error);
        MessageBox.alert('服务器暂时崩溃，请耐心等待或联系工作人员', '提示', {
          type: 'error'
        });
        next(false);
      }
    },
    hidden: true,
    children: [
      {
        path: 'list',
        name: '审核员信息列表',
        component: () => import('@/views/page/adminSuper/showSuper'),
        meta: { title: '审核员信息列表', icon: 'table' },
        hidden: true
      },
      {
        path: 'addSuper',
        name: '添加审核员',
        component: () => import('@/views/page/adminSuper/addSuper'),
        meta: { title: '添加审核员', icon: 'el-icon-thumb' },
        hidden: true
      },
      {
        path: 'updateSuper/:id',
        name: '修改审核信息',
        component: () => import('@/views/page/adminSuper/addSuper'),
        meta: { title: '修改审核信息', noCache: true },
        hidden: true
      }
    ]
  },
  // {
  //   path: '/reviewMaterials',
  //   component: Layout,
  //   redirect: '/reviewMaterials/list',
  //   name: '提交材料批阅',
  //   meta: { title: '提交材料批阅', icon: 'el-icon-s-help' },
  //   children: [
  //     {
  //       path: 'list',
  //       name: '提交材料批阅',
  //       component: () => import('@/views/page/superAdmin/form/review'),
  //       meta: { title: '提交材料批阅', icon: 'table' }
  //     },
  //   ]
  // },
  {
    path: '/nested',
    component: Layout,
    redirect: '/nested/menu1',
    name: 'Nested',
    beforeEnter: async (to, from, next) => {
      try {
        const response = await axios.post(`${process.env.VUE_APP_BASE_API}/admservice/adm-superadministrator/checkIfSuperAdmin`);
        console.log(response.data)
        if (response.data) {
          // 是超级管理员，可以访问
          next();
        } else {
          // 不是超级管理员，不能访问
          MessageBox.alert('权限不足', '提示', {
            type: 'error'
          });
          next(false);
        }
      } catch (error) {
        console.error(error);
        MessageBox.alert('服务器暂时崩溃，请耐心等待或联系工作人员', '提示', {
          type: 'error'
        });
        next(false);
      }
    },
    meta: {
      title: '超级管理员操作',
      icon: 'nested',
      // requiresAuth: true ,// 添加需要权限验证的标识
    },
    children: [
      {
        path: 'menu3',
        component: () => import('@/views/page/superAdmin/form/show'),
        name: 'Menu3',
        meta: { title: '历史批阅', icon: 'el-icon-s-grid' }
      },
      {
        path: 'menu2',
        component: () => import('@/views/page/superAdmin/form/proApplication'),
        name: 'Menu2',
        meta: { title: '项目申报', icon: 'el-icon-receiving' }
      },
      {
        path: 'list',
        name: '分类维护',
        component: () => import('@/views/page/categoryAdmin/showSuper'),
        meta: { title: '分类维护', icon: 'table' }
      },
      {
        path: 'signSome',
        name: '考勤迟到未签管理',
        component: () => import('@/views/member/someSign'),
        meta: { title: '考勤迟到未签管理', icon: 'table' }
      },
      {
        path: 'signAll',
        name: '考勤总览',
        component: () => import('@/views/member/allSign'),
        meta: { title: '考勤总览', icon: 'table' }
      },
      {
        path: '/adminTeacher',
        component: Layout,
        redirect: '/adminTeacher/list',
        name: '教师信息管理',
        meta: { title: '教师信息管理', icon: 'el-icon-s-help' },
        beforeEnter: async (to, from, next) => {
          try {
            const response = await axios.post(`${process.env.VUE_APP_BASE_API}/admservice/adm-superadministrator/checkIfSuperAdmin`);
            console.log(response.data)
            if (response.data) {
              // 是超级管理员，可以访问
              next();
            } else {
              // 不是超级管理员，不能访问
              MessageBox.alert('权限不足', '提示', {
                type: 'error'
              });
              next(false);
            }
          } catch (error) {
            console.error(error);
            MessageBox.alert('服务器暂时崩溃，请耐心等待或联系工作人员', '提示', {
              type: 'error'
            });
            next(false);
          }
        },
        children: [
          {
            path: 'list',
            name: '教师信息列表',
            component: () => import('@/views/page/adminTeacher/show'),
            meta: { title: '教师列表', icon: 'table' }
          },
          {
            path: 'addTeacher',
            name: '添加教师信息',
            component: () => import('@/views/page/adminTeacher/addTeacher'),
            meta: { title: '添加教师信息', icon: 'el-icon-thumb' }
          },
          {
            path: 'updateTeacher/:id',
            name: '修改教师信息',
            component: () => import('@/views/page/adminTeacher/addTeacher'),
            meta: { title: '修改教师信息', noCache: true },
            hidden: true
          }
        ]
      },
      {
        path: '/adminSuper',
        component: Layout,
        redirect: '/adminSuper/list',
        name: '审核人信息管理',
        meta: { title: '审核人信息管理', icon: 'el-icon-s-finance' },
        beforeEnter: async (to, from, next) => {
          try {
            const response = await axios.post(`${process.env.VUE_APP_BASE_API}/admservice/adm-superadministrator/checkIfSuperAdmin`);
            console.log(response.data)
            if (response.data) {
              // 是超级管理员，可以访问
              next();
            } else {
              // 不是超级管理员，不能访问
              MessageBox.alert('权限不足', '提示', {
                type: 'error'
              });
              next(false);
            }
          } catch (error) {
            console.error(error);
            MessageBox.alert('服务器暂时崩溃，请耐心等待或联系工作人员', '提示', {
              type: 'error'
            });
            next(false);
          }
        },
        children: [
          {
            path: 'list',
            name: '审核人列表',
            component: () => import('@/views/page/adminSuper/showSuper'),
            meta: { title: '审核人列表', icon: 'el-icon-menu' }
          },
          {
            path: 'addSuper',
            name: '添加审核人',
            component: () => import('@/views/page/adminSuper/addSuper'),
            meta: { title: '添加审核人', icon: 'el-icon-finished' }
          },
          {
            path: 'updateSuper/:id',
            name: '修改审核员信息',
            component: () => import('@/views/page/adminSuper/addSuper'),
            meta: { title: '修改审核员信息', noCache: true },
            hidden: true
          }
        ]
      },
      {
        path: '/genTeacher',
        component: Layout,
        redirect: '/gen/list',
        name: '总评管理',
        meta: { title: '总评管理', icon: 'el-icon-connection' },
        beforeEnter: async (to, from, next) => {
          try {
            const response = await axios.post(`${process.env.VUE_APP_BASE_API}/admservice/adm-superadministrator/checkIfSuperAdmin`);
            console.log(response.data)
            if (response.data) {
              // 是超级管理员，可以访问
              next();
            } else {
              // 不是超级管理员，不能访问
              MessageBox.alert('权限不足', '提示', {
                type: 'error'
              });
              next(false);
            }
          } catch (error) {
            console.error(error);
            MessageBox.alert('服务器暂时崩溃，请耐心等待或联系工作人员', '提示', {
              type: 'error'
            });
            next(false);
          }
        },
        children: [
          {
            path: 'menu4',
            component: () => import('@/views/page/superAdmin/general/madeGen'),
            name: 'Menu4',
            meta: { title: '生成总评', icon: 'el-icon-news' }
          },
          {
            path: 'menu5',
            component: () => import('@/views/page/superAdmin/general/showGen'),
            name: 'Menu5',
            meta: { title: '总评', icon: 'el-icon-trophy' }
          },
        ]
      }
    ]
  },

  // 404 page must be placed at the end !!!
  {path: '*', redirect: '/404', hidden: true}
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({y: 0}),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
